from django import template
from django.template.defaultfilters import stringfilter
import markdown as md


register = template.Library()

"""
Be careful when use django template filter:
1. https://stackoverflow.com/a/51033866/15172980
"""


@register.filter(name='get_item')
def get_item(value, arg):
    return value.get(arg)


@register.filter(name='markdown')
@stringfilter
def markdown(value):
    """
    Tutorial: https://learndjango.com/tutorials/django-markdown-tutorial
    """
    return md.markdown(value,
                       extensions=[
                           'markdown.extensions.extra',
                           'tables',
                           'markdown.extensions.codehilite',
                           'markdown.extensions.fenced_code',
                           'pymdownx.arithmatex',
                       ],
                       extension_configs={
                           'markdown_katex': {
                               'no_inline_svg': True,
                               'insert_fonts_css': True,
                           },
                           'pymdownx.arithmatex': {
                               'generic': True,
                           },
                           'markdown.extensions.codehilite': {
                               'linenums': True
                           }
                       })
